#include "virtualStream.h"
//#include "stream_model/model_mountStream.h"
#include <API/Types_Api_Stream.h>

//Problem: what about 

#define _ConvertToPub(from,to,num,offset)  Mtype_fun(ConvertToPub,from->_typeid,(from->object,to,num,offset))
#define _ConverToPrv(from,to,num,offset)   Mtype_fun(ConvertToPrv,to->_typeid,(from,to->object,num,offset))
#define _WriteStream(stream,input,num,offset) Mtype_fun(WriteStream,stream->_typeid,(stream->object,input,num,offset))
#define _ReadStream(stream,output,num,offset) Mtype_fun(ReadStream,stream->_typeid,(stream->object,output,num,offset))
#define _PeekStream(stream,output,num,offset) Mtype_fun(PeekStream,stream->_typeid,(stream->object,output,num,offset))
#define _SeekStream(stream,num)  Mtype_fun(SeekStream,stream->_typeid,(stream->object,num))
//#define _MountStream(stream,root) Mtype_fun(MountStream,root->_typeid,(stream->object,root->object))
//#define _UnmountStream(stream,root) Mtype_fun(UnmountStream,root->_typeid,(stream->object,root->object))
//#define _NameStream(streamName,stream) Mtype_fun(NameStream,stream->_typeid,(streamName,stream->object))
#define _IOStream(dfps,stream,num,offset) Mtype_fun(IOStream,stream->_typeid,(dfps,stream->object,num,offset))
#define _GetStreamLength(of)  Mtype_fun(GetStreamLength,of->_typeid,(of->object))
//#define _CombineStream(to,from)  Mtype_fun(CombineStream,to->_typeid,(to->object,from->object))
//There is problem, for the different virtualStream is pointed to different stream. it can't be combined.
//Should use ConvertToPub then combine the ConvertToPrv
//ApartStream is the same
#define _EndStream(target)  Mtype_fun(EndStream,target->_typeid,(target->object))

static inline _ptlong _MountStream(strType stream,strType root)
{
  return 1;
}
static inline _ptlong _UnmountStream(strType stream, strType root)
{
  return 1;
}

static inline _ptlong _NameStream(char* streamName,strType stream)
{
  return 1;
}

static strType _CombineStream(strType to, strType from)
{
  _dfps to_ = _DFPSOP(CreateStream)();
  _dfps from_ = _DFPSOP(CreateStream)();
  _ConvertToPub(to,to_,UNLIMITED,0);
  _ConvertToPub(from,from_,UNLIMITED,0);
  _DFPSOP(CombineStream)(to_,from_);
  _ConvertToPrv(to_,to,UNLIMITED,0);
  _DFPSOP(EndStream)(to_);
  _DFPSOP(EndStream)(from_);
  return to;
}

static strType _ApartStream(strType from, strType to, _length partpoint)
{
  _dfps to_ = _DFPSOP(CreateStream)();
  _dfps from_ = _DFPSOP(CreateStream)();
  _ConvertToPub(from,to_,UNLIMITED,0);
  _DFPSOP(ApartStream)(to_,from_,partpoint);
  _ConvertToPrv(to_,from,UNLIMITED,0);
  _ConvertToPrv(from_,to,UNLIMITED,0);
  _DFPSOP(EndStream)(to_);
  _DFPSOP(EndStream)(from_);
  return from;
}

int STRFUNC(PointToStream)(strType stream, unsigned int _typeid, void* obj)
{
  stream->_typeid = _typeid;
  stream->object = obj;
}
